package com.company.modules.project.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author : Real
 * @date : 2022/1/24 14:04
 * @description : HRM 系统工具类
 */
public class HrmUtil {

    /**
     * MD5 加密，方便后续保存
     *
     * @param originalMessage 原始信息
     * @return 加密之后的数据
     */
    public static String md5(String originalMessage) {
        if (StringUtils.isBlank(originalMessage)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(originalMessage.getBytes());
    }

    /**
     * 生成 UUID
     *
     * @return UUID 字符串
     */
    public static String generatorUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 生成 JSON 字符串
     *
     * @param result 结果字符串
     * @param map    map对象
     * @return JSON字符串
     */
    public static String getJSONString(String result, Map<String, Object> map) {
        map.put("result", result);
        return JSON.toJSONString(map);
    }

    /**
     * 生成验证码
     *
     * @return verify code
     */
    public static String generateVerifyCode() {
        String[] codeList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(codeList.length);
            String temp = codeList[index];
            sb.append(temp);
        }
        return sb.toString();
    }

    /**
     * generate verify code by length
     *
     * @param length length
     * @return verify code
     */
    public static String generateVerifyCode(int length) {
        String[] codeList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(codeList.length);
            String temp = codeList[index];
            sb.append(temp);
        }
        return sb.toString();
    }

    public static String generateNumberVerifyCode(int length) {
        String[] list = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuffer sb  = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(list.length);
            String temp = list[index];
            sb.append(temp);
        }
        return sb.toString();
    }

    public static String generateSalt() {
        String[] codeList = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            int index = random.nextInt(codeList.length);
            String temp = codeList[index];
            sb.append(temp);
        }
        return sb.toString();
    }

}
